/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logica.jpaControllers;

import Entidades.Remito;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entidades.Proveedor;
import Entidades.Contrato;
import Entidades.Transporte;
import Entidades.Detalleremito;
import java.util.ArrayList;
import java.util.Collection;
import Entidades.Recepcion;
import logica.jpaControllers.exceptions.IllegalOrphanException;
import logica.jpaControllers.exceptions.NonexistentEntityException;

/**
 *
 * @author Anali
 */
public class RemitoJpaController {

    public RemitoJpaController() {
        emf = Persistence.createEntityManagerFactory("GestorPesajeJpaPU2");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Remito remito) {
        if (remito.getDetalleremitoCollection() == null) {
            remito.setDetalleremitoCollection(new ArrayList<Detalleremito>());
        }
        if (remito.getRecepcionCollection() == null) {
            remito.setRecepcionCollection(new ArrayList<Recepcion>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Proveedor proveedor = remito.getProveedor();
            if (proveedor != null) {
                proveedor = em.getReference(proveedor.getClass(), proveedor.getIdProveedor());
                remito.setProveedor(proveedor);
            }
            Contrato contrato = remito.getContrato();
            if (contrato != null) {
                contrato = em.getReference(contrato.getClass(), contrato.getNumeroContrato());
                remito.setContrato(contrato);
            }
            Transporte transporte = remito.getTransporte();
            if (transporte != null) {
                transporte = em.getReference(transporte.getClass(), transporte.getIdTransporte());
                remito.setTransporte(transporte);
            }
            Collection<Detalleremito> attachedDetalleremitoCollection = new ArrayList<Detalleremito>();
            for (Detalleremito detalleremitoCollectionDetalleremitoToAttach : remito.getDetalleremitoCollection()) {
                detalleremitoCollectionDetalleremitoToAttach = em.getReference(detalleremitoCollectionDetalleremitoToAttach.getClass(), detalleremitoCollectionDetalleremitoToAttach.getIdDetalleRemito());
                attachedDetalleremitoCollection.add(detalleremitoCollectionDetalleremitoToAttach);
            }
            remito.setDetalleremitoCollection(attachedDetalleremitoCollection);
            Collection<Recepcion> attachedRecepcionCollection = new ArrayList<Recepcion>();
            for (Recepcion recepcionCollectionRecepcionToAttach : remito.getRecepcionCollection()) {
                recepcionCollectionRecepcionToAttach = em.getReference(recepcionCollectionRecepcionToAttach.getClass(), recepcionCollectionRecepcionToAttach.getNumeroRecepcion());
                attachedRecepcionCollection.add(recepcionCollectionRecepcionToAttach);
            }
            remito.setRecepcionCollection(attachedRecepcionCollection);
            em.persist(remito);
            if (proveedor != null) {
                proveedor.getRemitoCollection().add(remito);
                proveedor = em.merge(proveedor);
            }
            if (contrato != null) {
                contrato.getRemitoCollection().add(remito);
                contrato = em.merge(contrato);
            }
            if (transporte != null) {
                transporte.getRemitoCollection().add(remito);
                transporte = em.merge(transporte);
            }
            for (Detalleremito detalleremitoCollectionDetalleremito : remito.getDetalleremitoCollection()) {
                Remito oldRemitoOfDetalleremitoCollectionDetalleremito = detalleremitoCollectionDetalleremito.getRemito();
                detalleremitoCollectionDetalleremito.setRemito(remito);
                detalleremitoCollectionDetalleremito = em.merge(detalleremitoCollectionDetalleremito);
                if (oldRemitoOfDetalleremitoCollectionDetalleremito != null) {
                    oldRemitoOfDetalleremitoCollectionDetalleremito.getDetalleremitoCollection().remove(detalleremitoCollectionDetalleremito);
                    oldRemitoOfDetalleremitoCollectionDetalleremito = em.merge(oldRemitoOfDetalleremitoCollectionDetalleremito);
                }
            }
            for (Recepcion recepcionCollectionRecepcion : remito.getRecepcionCollection()) {
                Remito oldRemitoOfRecepcionCollectionRecepcion = recepcionCollectionRecepcion.getRemito();
                recepcionCollectionRecepcion.setRemito(remito);
                recepcionCollectionRecepcion = em.merge(recepcionCollectionRecepcion);
                if (oldRemitoOfRecepcionCollectionRecepcion != null) {
                    oldRemitoOfRecepcionCollectionRecepcion.getRecepcionCollection().remove(recepcionCollectionRecepcion);
                    oldRemitoOfRecepcionCollectionRecepcion = em.merge(oldRemitoOfRecepcionCollectionRecepcion);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Remito remito) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Remito persistentRemito = em.find(Remito.class, remito.getIdRemito());
            Proveedor proveedorOld = persistentRemito.getProveedor();
            Proveedor proveedorNew = remito.getProveedor();
            Contrato contratoOld = persistentRemito.getContrato();
            Contrato contratoNew = remito.getContrato();
            Transporte transporteOld = persistentRemito.getTransporte();
            Transporte transporteNew = remito.getTransporte();
            Collection<Detalleremito> detalleremitoCollectionOld = persistentRemito.getDetalleremitoCollection();
            Collection<Detalleremito> detalleremitoCollectionNew = remito.getDetalleremitoCollection();
            Collection<Recepcion> recepcionCollectionOld = persistentRemito.getRecepcionCollection();
            Collection<Recepcion> recepcionCollectionNew = remito.getRecepcionCollection();
            List<String> illegalOrphanMessages = null;
            for (Recepcion recepcionCollectionOldRecepcion : recepcionCollectionOld) {
                if (!recepcionCollectionNew.contains(recepcionCollectionOldRecepcion)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Recepcion " + recepcionCollectionOldRecepcion + " since its remito field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (proveedorNew != null) {
                proveedorNew = em.getReference(proveedorNew.getClass(), proveedorNew.getIdProveedor());
                remito.setProveedor(proveedorNew);
            }
            if (contratoNew != null) {
                contratoNew = em.getReference(contratoNew.getClass(), contratoNew.getNumeroContrato());
                remito.setContrato(contratoNew);
            }
            if (transporteNew != null) {
                transporteNew = em.getReference(transporteNew.getClass(), transporteNew.getIdTransporte());
                remito.setTransporte(transporteNew);
            }
            Collection<Detalleremito> attachedDetalleremitoCollectionNew = new ArrayList<Detalleremito>();
            for (Detalleremito detalleremitoCollectionNewDetalleremitoToAttach : detalleremitoCollectionNew) {
                detalleremitoCollectionNewDetalleremitoToAttach = em.getReference(detalleremitoCollectionNewDetalleremitoToAttach.getClass(), detalleremitoCollectionNewDetalleremitoToAttach.getIdDetalleRemito());
                attachedDetalleremitoCollectionNew.add(detalleremitoCollectionNewDetalleremitoToAttach);
            }
            detalleremitoCollectionNew = attachedDetalleremitoCollectionNew;
            remito.setDetalleremitoCollection(detalleremitoCollectionNew);
            Collection<Recepcion> attachedRecepcionCollectionNew = new ArrayList<Recepcion>();
            for (Recepcion recepcionCollectionNewRecepcionToAttach : recepcionCollectionNew) {
                recepcionCollectionNewRecepcionToAttach = em.getReference(recepcionCollectionNewRecepcionToAttach.getClass(), recepcionCollectionNewRecepcionToAttach.getNumeroRecepcion());
                attachedRecepcionCollectionNew.add(recepcionCollectionNewRecepcionToAttach);
            }
            recepcionCollectionNew = attachedRecepcionCollectionNew;
            remito.setRecepcionCollection(recepcionCollectionNew);
            remito = em.merge(remito);
            if (proveedorOld != null && !proveedorOld.equals(proveedorNew)) {
                proveedorOld.getRemitoCollection().remove(remito);
                proveedorOld = em.merge(proveedorOld);
            }
            if (proveedorNew != null && !proveedorNew.equals(proveedorOld)) {
                proveedorNew.getRemitoCollection().add(remito);
                proveedorNew = em.merge(proveedorNew);
            }
            if (contratoOld != null && !contratoOld.equals(contratoNew)) {
                contratoOld.getRemitoCollection().remove(remito);
                contratoOld = em.merge(contratoOld);
            }
            if (contratoNew != null && !contratoNew.equals(contratoOld)) {
                contratoNew.getRemitoCollection().add(remito);
                contratoNew = em.merge(contratoNew);
            }
            if (transporteOld != null && !transporteOld.equals(transporteNew)) {
                transporteOld.getRemitoCollection().remove(remito);
                transporteOld = em.merge(transporteOld);
            }
            if (transporteNew != null && !transporteNew.equals(transporteOld)) {
                transporteNew.getRemitoCollection().add(remito);
                transporteNew = em.merge(transporteNew);
            }
            for (Detalleremito detalleremitoCollectionOldDetalleremito : detalleremitoCollectionOld) {
                if (!detalleremitoCollectionNew.contains(detalleremitoCollectionOldDetalleremito)) {
                    detalleremitoCollectionOldDetalleremito.setRemito(null);
                    detalleremitoCollectionOldDetalleremito = em.merge(detalleremitoCollectionOldDetalleremito);
                }
            }
            for (Detalleremito detalleremitoCollectionNewDetalleremito : detalleremitoCollectionNew) {
                if (!detalleremitoCollectionOld.contains(detalleremitoCollectionNewDetalleremito)) {
                    Remito oldRemitoOfDetalleremitoCollectionNewDetalleremito = detalleremitoCollectionNewDetalleremito.getRemito();
                    detalleremitoCollectionNewDetalleremito.setRemito(remito);
                    detalleremitoCollectionNewDetalleremito = em.merge(detalleremitoCollectionNewDetalleremito);
                    if (oldRemitoOfDetalleremitoCollectionNewDetalleremito != null && !oldRemitoOfDetalleremitoCollectionNewDetalleremito.equals(remito)) {
                        oldRemitoOfDetalleremitoCollectionNewDetalleremito.getDetalleremitoCollection().remove(detalleremitoCollectionNewDetalleremito);
                        oldRemitoOfDetalleremitoCollectionNewDetalleremito = em.merge(oldRemitoOfDetalleremitoCollectionNewDetalleremito);
                    }
                }
            }
            for (Recepcion recepcionCollectionNewRecepcion : recepcionCollectionNew) {
                if (!recepcionCollectionOld.contains(recepcionCollectionNewRecepcion)) {
                    Remito oldRemitoOfRecepcionCollectionNewRecepcion = recepcionCollectionNewRecepcion.getRemito();
                    recepcionCollectionNewRecepcion.setRemito(remito);
                    recepcionCollectionNewRecepcion = em.merge(recepcionCollectionNewRecepcion);
                    if (oldRemitoOfRecepcionCollectionNewRecepcion != null && !oldRemitoOfRecepcionCollectionNewRecepcion.equals(remito)) {
                        oldRemitoOfRecepcionCollectionNewRecepcion.getRecepcionCollection().remove(recepcionCollectionNewRecepcion);
                        oldRemitoOfRecepcionCollectionNewRecepcion = em.merge(oldRemitoOfRecepcionCollectionNewRecepcion);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = remito.getIdRemito();
                if (findRemito(id) == null) {
                    throw new NonexistentEntityException("The remito with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Remito remito;
            try {
                remito = em.getReference(Remito.class, id);
                remito.getIdRemito();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The remito with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Recepcion> recepcionCollectionOrphanCheck = remito.getRecepcionCollection();
            for (Recepcion recepcionCollectionOrphanCheckRecepcion : recepcionCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Remito (" + remito + ") cannot be destroyed since the Recepcion " + recepcionCollectionOrphanCheckRecepcion + " in its recepcionCollection field has a non-nullable remito field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Proveedor proveedor = remito.getProveedor();
            if (proveedor != null) {
                proveedor.getRemitoCollection().remove(remito);
                proveedor = em.merge(proveedor);
            }
            Contrato contrato = remito.getContrato();
            if (contrato != null) {
                contrato.getRemitoCollection().remove(remito);
                contrato = em.merge(contrato);
            }
            Transporte transporte = remito.getTransporte();
            if (transporte != null) {
                transporte.getRemitoCollection().remove(remito);
                transporte = em.merge(transporte);
            }
            Collection<Detalleremito> detalleremitoCollection = remito.getDetalleremitoCollection();
            for (Detalleremito detalleremitoCollectionDetalleremito : detalleremitoCollection) {
                detalleremitoCollectionDetalleremito.setRemito(null);
                detalleremitoCollectionDetalleremito = em.merge(detalleremitoCollectionDetalleremito);
            }
            em.remove(remito);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Remito> findRemitoEntities() {
        return findRemitoEntities(true, -1, -1);
    }

    public List<Remito> findRemitoEntities(int maxResults, int firstResult) {
        return findRemitoEntities(false, maxResults, firstResult);
    }

    private List<Remito> findRemitoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Remito as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Remito findRemito(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Remito.class, id);
        } finally {
            em.close();
        }
    }

    public int getRemitoCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Remito as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
